package com.ruoyi.admin.service.impl;

import com.ruoyi.admin.domain.Course;
import com.ruoyi.admin.domain.LabCourse;
import com.ruoyi.admin.domain.Schedule;
import com.ruoyi.admin.domain.Semester;
import com.ruoyi.admin.domain.dto.CourseScheduleDto;
import com.ruoyi.admin.domain.vo.CourseScheduleVo;
import com.ruoyi.admin.mapper.LabscheduleMapper;
import com.ruoyi.admin.mapper.SemesterMapper;
import com.ruoyi.admin.service.IIndexService;
import com.ruoyi.common.utils.bean.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class IIndexServiceImpl implements IIndexService {
    @Resource
    private LabscheduleMapper scheduleMapper;
    @Resource
    private SemesterMapper semesterMapper;
    @Override
    public List<Schedule> getCourse(CourseScheduleDto courseScheduleDto) {
        Semester semester =semesterMapper.selectCurrentSemester();
        Long semesterId = semester.getSemesterId();
        //前置：查询当前学期
        courseScheduleDto.setSemesterId(semesterId);
        //1. 根据当前学期，周次，查出正在上的实验课有哪些。（查排课表）
        List<CourseScheduleVo> courseScheduleVos = scheduleMapper.selectLabSchedule(courseScheduleDto);

        List<Schedule>schedules=new ArrayList<>(courseScheduleVos.size());

        //对查询出来的vo分类，将1的数据，根据实验室类型，按照实验室编号进行排序
        List<LabCourse> labCourses = transferFormat(courseScheduleVos);
        Schedule schedule = new Schedule();
        schedule.setSemester(semester.getTerm());
        schedule.setWeek(courseScheduleDto.getDayOfWeek());
        schedule.setWeekNum(courseScheduleDto.getWeek());
        schedule.setLabList(labCourses);
        schedules.add(schedule);

        return  schedules;
    }

    /**
     * 转换课程表查询出的数据的格式
     */
    private List<LabCourse>transferFormat(List<CourseScheduleVo>courseScheduleVos){
        log.info("处理前的数据大小是{}",courseScheduleVos.size());
        //分类
        Map<String, Map<Integer, List<Course>>> groupedData = new HashMap<>();
        for (CourseScheduleVo labData : courseScheduleVos) {
            int labNumber = labData.getId();
            String labCategory = labData.getLabType();
            Course course = new Course();
            BeanUtils.copyBeanProp(course,labData);
            // 获取当前 labCategory 下的 labNumber 分类的 Map
            Map<Integer, List<Course>> labNumberMap = groupedData.getOrDefault(labCategory, new HashMap<>());
            // 获取当前 labNumber 和 labCategory 组合下的 LabData 列表
            List<Course> labCategoryList = labNumberMap.getOrDefault(labNumber, new ArrayList<>());
            // 将当前 LabData 添加到列表中
            labCategoryList.add(course);
            // 更新 labCategory下的Map
            labNumberMap.put(labNumber, labCategoryList);
            // 更新 groupedData
            groupedData.put(labCategory, labNumberMap);
        }
        //归并
        List<LabCourse>labCourses=new ArrayList<>();
        for(Map.Entry<String,Map<Integer,List<Course>>>map:groupedData.entrySet()){
            Map<Integer, List<Course>> value = map.getValue();
            for(Map.Entry<Integer,List<Course>>item:value.entrySet()){
                LabCourse labCourse = new LabCourse();
                //每种类型下有不同的实验室编号
                labCourse.setLabType(map.getKey());
                labCourse.setId(item.getKey());
                labCourse.setCourseList(item.getValue());
                labCourses.add(labCourse);
            }
        }
        log.info("处理后的数据大小是{}",labCourses.size());
        return labCourses;
    }




}
